﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using KerriganMobile.MobileServiceWCF;
using System.Collections.ObjectModel;
using System.ComponentModel;
using KerriganMobile.Models;
using System.Diagnostics;

namespace KerriganMobile.Views
{
    public partial class CustomerList : PhoneApplicationPage
    {
        private CustomerListVO vo;
        public CustomerList()
        {
            InitializeComponent();
            vo = new CustomerListVO();
            DataContext = vo;


        }

        void client_GetAllCustomerCompleted(object sender, GetAllCustomerCompletedEventArgs e)
        {
            vo.CustomerList = e.Result;
        }


        private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CustomerByInstructorByDate_Result customer = (sender as ListBox).SelectedItem as CustomerByInstructorByDate_Result;
            if(customer != null){
                ApplicationManager.ID_CURRENT_CUSTOMER = customer.IdCustomer.Value;
                ApplicationManager.changeURI(this, "SessionList");
            }

        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

            ApplicationManager.client.GetAllCustomerCompleted += new EventHandler<GetAllCustomerCompletedEventArgs>(client_GetAllCustomerCompleted);
            ApplicationManager.client.GetAllCustomerAsync(ApplicationManager.ID_CURRENT_INSTRUCTOR);
            Debug.WriteLine("Loaded CustomerList");
        }


        private void search_Click(object sender, RoutedEventArgs e)
        {
            if (this.vo.CopyCustomerList == null)
            {
                this.vo.CopyCustomerList = this.vo.CustomerList;
            }
            if (this.vo.SearchName != "")
            {
                ObservableCollection<CustomerByInstructorByDate_Result> temp = new ObservableCollection<CustomerByInstructorByDate_Result>();
                foreach (CustomerByInstructorByDate_Result customer in this.vo.CopyCustomerList)
                {
                    if (customer.LastName.ToLower().Contains(this.vo.SearchName.ToLower()) || customer.FirstName.ToLower().Contains(this.vo.SearchName.ToLower()))
                    {
                        temp.Add(customer);
                    }
                }
                this.vo.CustomerList = temp;
            }
            else
            {
                this.vo.CustomerList = this.vo.CopyCustomerList;
            }
        }
    }


    public class CustomerListVO : INotifyPropertyChanged
    {
        public CustomerListVO()
        {
            CustomerList = new ObservableCollection<CustomerByInstructorByDate_Result>();
        }


        private string _searchName;

        public string SearchName
        {
            get
            {
                return this._searchName;
            }
            set
            {
                this._searchName = value;

                OnPropertyChanged("SearchName");
            }
        }

        public ObservableCollection<CustomerByInstructorByDate_Result> CopyCustomerList { get; set; }

        private ObservableCollection<CustomerByInstructorByDate_Result> _customerList;


        public ObservableCollection<CustomerByInstructorByDate_Result> CustomerList
        {             
            get
            {
                return this._customerList;
            }
            set
            {
                this._customerList = value;
                OnPropertyChanged("CustomerList");
            } 
        
        }


        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }


    }


}